package cn.virens.dd.simple.value;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Stream;

import cn.hutool.core.convert.Convert;
import cn.hutool.core.util.ArrayUtil;
import cn.virens.dd.StrategyValue;

public class StrategyValueByLumen implements StrategyValue {
	private final int[] lumens;

	public StrategyValueByLumen(String... lumens) {
		this(Arrays.asList(lumens));
	}

	public StrategyValueByLumen(List<String> lumens) {
		this.lumens = lumens.stream().flatMap((v) -> {
			return StrategyValueByLumen.split(v);
		}).mapToInt((v) -> {
			return Convert.toInt(v, -1);
		}).filter((v) -> {
			return v >= 0;
		}).toArray();
	}

	public StrategyValueByLumen(StrategyValueByLumen other) {
		this.lumens = ArrayUtil.clone(other.lumens);
	}

	public int[] getLumens() {
		return lumens;
	}

	/** 根据权重判定是否符合条件(>=50%) */
	public boolean judgeWithWeight(Integer value) {
		int weight = 0;

		// 对当前变量值中的所有亮度值进行比对
		for (int lumen : lumens) {
			if (lumen < value) weight++;
		}

		// 判断权重 是否 大于数值总数的一半(>50%)
		if (weight < lumens.length / 2) {
			return Boolean.FALSE;
		} else {
			return Boolean.TRUE;
		}
	}

	private static Stream<String> split(String str) {
		if (str != null && !str.isEmpty()) {
			return Stream.of(str.split(","));
		} else {
			return Stream.empty();
		}
	}
}
